package com.example.himalaya.data;

import com.example.himalaya.utils.Constants;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;
import com.ximalaya.ting.android.opensdk.model.album.SearchAlbumList;
import com.ximalaya.ting.android.opensdk.model.track.TrackList;
import com.ximalaya.ting.android.opensdk.model.word.HotWordList;
import com.ximalaya.ting.android.opensdk.model.word.SuggestWords;

import java.util.HashMap;
import java.util.Map;

/**
 * Mode数据层
 */
public class XimalayaApi {

    private static XimalayaApi mXimalayaApi;

    private XimalayaApi() {
    }

    public static XimalayaApi getmXimalayaApi() {
        if (mXimalayaApi==null) {
            synchronized (XimalayaApi.class) {
                if (mXimalayaApi==null) {
                    mXimalayaApi = new XimalayaApi();
                }
            }
        }
        return mXimalayaApi;
    }

    /**
     * 获取推荐内容
     * @param callBack 请求结果的回调接口
     */
    public void getRecommendList(IDataCallBack<GussLikeAlbumList> callBack) {
        Map<String, String> map = new HashMap<>();//利用Map,封装参数
        //第二个参数，表示一页数据返回多少条专辑条目
        map.put(DTransferConstants.LIKE_COUNT, String.valueOf(Constants.COUNT_RECOMMEND));
        CommonRequest.getGuessLikeAlbum(map, callBack);
    }

    /**
     * 获取专辑详情
     * @param callBack 请求结果的回调接口
     * @param albumId 专辑ID
     * @param pageIndex 页码
     */
    public void getAlbumDetail(long albumId,int pageIndex,IDataCallBack<TrackList> callBack) {
        //根据专辑Id和页码,通过API请求，获得曲目列表
        Map<String, String> map = new HashMap<String, String>();
        map.put(DTransferConstants.ALBUM_ID, albumId+"");
        map.put(DTransferConstants.SORT, "asc");
        map.put(DTransferConstants.PAGE, pageIndex+"");
        map.put(DTransferConstants.PAGE_SIZE, Constants.COUNT_TRACK +"");
        CommonRequest.getTracks(map, callBack);
    }


    /**
     * 根据关键字进行搜索
     * @param keyword
     * @param pageNum
     * @param callBack
     */
    public void searchByKeyword(String keyword, int pageNum, IDataCallBack<SearchAlbumList> callBack) {
        Map<String, String> map = new HashMap<>();
        map.put(DTransferConstants.SEARCH_KEY, keyword);
        map.put(DTransferConstants.PAGE, pageNum+"");
        map.put(DTransferConstants.PAGE_SIZE, Constants.COUNT_ALBUM+"");
        CommonRequest.getSearchedAlbums(map, callBack);

    }

    /**
     * 获取热词
     * @param callBack
     */
    public void getHotWords(IDataCallBack<HotWordList> callBack) {
        Map<String, String> map = new HashMap<>();
        map.put(DTransferConstants.TOP, Constants.COUNT_HOT_WORD+"");
        CommonRequest.getHotWords(map, callBack);
    }

    /**
     * 根据关键字获取联想词
     * @param keyword
     * @param callBack
     */
    public void getSuggestWord(String keyword,IDataCallBack<SuggestWords> callBack)  {
        Map<String, String> map = new HashMap<>();
        map.put(DTransferConstants.SEARCH_KEY, keyword);
        CommonRequest.getSuggestWord(map, callBack);
    }
}
